package com.jeeplus.config.web;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hu
 * @Date 2020/4/2 10:29
 */
@Configuration
public class RabbitMqConfig {

    @Value("${spring.rabbitmq.queue.airRequest}")
    private String airRequest;

    @Value("${spring.rabbitmq.queue.oilRequest}")
    private String oilRequest;

    @Value("${spring.rabbitmq.queue.envRequest}")
    private String envRequest;

    @Value("${spring.rabbitmq.queue.infRequest}")
    private String infRequest;

    @Value("${spring.rabbitmq.exchange.airExchange}")
    private String airExchange;

    @Value("${spring.rabbitmq.exchange.oilExchange}")
    private String oilExchange;

    @Value("${spring.rabbitmq.exchange.envExchange}")
    private String envExchange;

    @Value("${spring.rabbitmq.exchange.infExchange}")
    private String infExchange;


    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(20);  //设置线程数
        factory.setMaxConcurrentConsumers(20); //最大线程数
        factory.setPrefetchCount(20);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    /**
     * 功能描述 序列化
     *
     * @author hgx
     * @date 2020/4/3 13:43
     */
    @Bean
    public Jackson2JavaTypeMapper javaTypeMapper() {
        Map<String, String> classIdMapping = new HashMap<>();
        DefaultJackson2JavaTypeMapper mapper = new DefaultJackson2JavaTypeMapper() {
            @Override
            protected String retrieveHeaderAsString(MessageProperties properties, String headerName) {
                String classId = super.retrieveHeaderAsString(properties, headerName);
                return classIdMapping.containsKey(classId) ? classIdMapping.get(classId) : classId;
            }
        };
        mapper.setTrustedPackages("*");
        return mapper;
    }

    @Bean
    public MessageConverter messageConverter(ObjectMapper objectMapper) {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(objectMapper);
        converter.setJavaTypeMapper(javaTypeMapper());
        return converter;
    }

    /**
     * 功能描述 创建队列
     *
     * @author hgx
     * @date 2020/4/2 11:12
     */
    @Bean
    public Queue getAirRequest() {
        return new Queue(airRequest);
    }

    @Bean
    public Queue getOilRequest() {
        return new Queue(oilRequest);
    }

    @Bean
    public Queue getEnvRequest() {
        return new Queue(envRequest);
    }

    @Bean
    public Queue getInfRequest() {
        return new Queue(infRequest);
    }


    /**
     * 功能描述 创建交换机
     *
     * @author hgx
     * @date 2020/4/2 11:12
     */
    @Bean
    public DirectExchange getExChange() {
        return new DirectExchange(airExchange);
    }

    @Bean
    public DirectExchange getOilExchange() {
        return new DirectExchange(oilExchange);
    }

    @Bean
    public DirectExchange getEnvExchange() {
        return new DirectExchange(envExchange);
    }

    @Bean
    public DirectExchange getInfExchange() {
        return new DirectExchange(infExchange);
    }

    /**
     * 功能描述 队列与交换机绑定
     * 队列 to 交换机
     * 用队列名称作为binding key
     *
     * @author hgx
     * @date 2020/4/2 11:04
     */
    @Bean
    public Binding bindingAirRequestExChange() {
        return BindingBuilder.bind(getAirRequest()).to(getExChange()).withQueueName();
    }

    @Bean
    public Binding bindingOilRequestExchange() {
        return BindingBuilder.bind(getOilRequest()).to(getOilExchange()).withQueueName();
    }

    @Bean
    public Binding bindingEnvRequestExchange() {
        return BindingBuilder.bind(getEnvRequest()).to(getEnvExchange()).withQueueName();
    }

    @Bean
    public Binding bindingInfRequestExchange() {
        return BindingBuilder.bind(getInfRequest()).to(getInfExchange()).withQueueName();
    }


}
